package  code.tools.bitmap
{
	import code.structure.manager.bitmap.MaterialBitmapCoverManager;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	
	/**
	 * 位图动画
	 * @author Yuri
	 * 
	 */
	public class MovieBitMap extends Bitmap
	{
		public function MovieBitMap(frame:int=24)
		{
			super();
			_moviePlayer = new MoviePlayer(this,null,0,0,int(1000/frame));
			initListeners();
		}
		
		protected var _moviePlayer:MoviePlayer;
		
		protected function initListeners():void
		{
			_moviePlayer.addEventListener(MoviePlayer.PLAY_COMP,onPlayerComplete);
			
		}
		
		protected function onPlayerComplete(event:Event):void
		{
			dispatchEvent(new Event(MoviePlayer.PLAY_COMP));
		}
		
		private var currentSubKey:String;
		private var lastSubKey:String;
		/**
		 *刷新素材 
		 * @param id
		 * @param subKey
		 * 
		 */
		public function refreshMaterial(id:*,subKey:String,loadType:int=0,type:String="swf"):void{
			currentSubKey = subKey;
			MaterialBitmapCoverManager.getInstance().getMaterial(id,subKey,onCovertCompleteFun);
		}
		
		/**
		 *获取素材信息 
		 * @param arr
		 * 
		 */
		private function onCovertCompleteFun(bitmapArr:Array):void
		{
			// TODO Auto Generated method stub
			_moviePlayer.dataArray = bitmapArr;
			_moviePlayer.start();
			updateLayerOut();
		}
		
		/**
		 *更新布局 
		 * 
		 */
		public function updateLayerOut():void{
			
		}
		
		public function stop():void
		{
			if (_moviePlayer)
				_moviePlayer.stop();
		}
		
		public function play():void
		{
			if (_moviePlayer)
				_moviePlayer.play();
		}
		
		public function set bitmapDataList(value:Array):void{
			_moviePlayer.dataArray = value;
			_moviePlayer.start();
			updateLayerOut();
		}
		
		/**
		 * 設置幀頻 
		 * @param value
		 * 
		 */
		public function set interval(value:int):void
		{
			_moviePlayer.interval = value;
		}
		
		public function get moviePlayer():MoviePlayer
		{
			return _moviePlayer;
		}
		
		/**
		 *清空对象 
		 * 
		 */
		public function clear():void{
			if(_moviePlayer){
				_moviePlayer.stop();
			}
			this.bitmapData = null;
		}
		
		/**
		 *销毁 对象
		 * 
		 */
		public function destroy():void{
			if(_moviePlayer){
				_moviePlayer.stop();
				_moviePlayer = null;
			}
			clear();
		}

	}
}